package com.jbm.jbmsupplier.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.*
import android.widget.BaseAdapter
import com.jbm.core.iface.IAcross
import com.jbm.jbmsupplier.R
import com.jbm.jbmsupplier.activity.rxjavanet.ModeRxjavaNet
import com.jbm.jbmsupplier.view.DialogLoading
import com.jbm.jbmsupplier.view.DoubleButtonDialog
import com.jbm.onehome.iface.ISeller
import com.jbm.onehome.iface.LoginEntity
import com.jbm.onehome.iface.SellerEntity
import rx.Observable
import rx.Observer
import java.util.*

/**
 * Created by 家帮忙 on 2017/8/25.
 */
class Perm_Manage_Adapter : BaseAdapter{
    var content:Context?=null;
    var listperm: ArrayList<LoginEntity>?=null;
    var pri =ArrayList<String>();

    constructor(content: Context?, listperm: ArrayList<LoginEntity>?) : super() {
        this.content = content
        this.listperm = listperm

        dialogLoading = DialogLoading(content!!, "加载中.....", false)
    }



    override fun getItem(position: Int): Any {
        return listperm!![position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getCount(): Int {
      return  listperm!!.size
    }


     var  positions: Int?=null

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var premviewhoder: PremViewHoder
        var convertViews=convertView
        if (convertViews==null) {
            convertViews = View.inflate(content, R.layout.searchmanage_listitem, null);
            premviewhoder=PremViewHoder(convertViews)
            convertViews.setTag(premviewhoder)
        }else{
            premviewhoder= convertViews.getTag() as PremViewHoder
        }
        initDate(listperm!!,position,premviewhoder)
         positions = position
        return convertViews!!
    }
     var  premviewhoders: Perm_Manage_Adapter.PremViewHoder?=null
    fun initDate(listperm:ArrayList<LoginEntity>, position:Int, premviewhoder: PremViewHoder){
        if(listperm.get(position).logo!=null){
        ModeRxjavaNet.getInstantMode().PicassIma(content!!,listperm.get(position).logo, premviewhoder.text_perm_image)}

        premviewhoder.text_perm_name?.text=listperm.get(position).nickname/*+"\n"+listperm.get(position).accSecretEntity.mobile*/
        if (listperm.get(position).privilegeList!=null){
        for(i in listperm.get(position).privilegeList.indices){
            if (listperm.get(position).privilegeList.get(i).priCode.equals("ReportJob")){//报修
                premviewhoder.image_prem_ludan?.isChecked=true
            } else if (listperm.get(position).privilegeList.get(i).priCode.equals("CustomSvr")){//客服服务
                premviewhoder.image_prem_kefu?.isChecked=true
            }else if (listperm.get(position).privilegeList.get(i).priCode.equals("Worker")){//作业
                premviewhoder.image_prem_shigongdui?.isChecked=true
            }else if (listperm.get(position).privilegeList.get(i).priCode.equals("AssManager")||listperm.get(position).privilegeList.get(i).priCode.equals("JobAss")){//副经理
                premviewhoder.image_prem_fuguanli?.isChecked=true
            }
         }
        }
        premviewhoders=premviewhoder
        premviewhoder.image_prem_ludan?.setTag(position)
        premviewhoder.image_prem_kefu?.setTag(position)
        premviewhoder.image_prem_shigongdui?.setTag(position)
        premviewhoder.image_prem_fuguanli?.setTag(position)
        premviewhoder.image_prem_ludan?.setOnCheckedChangeListener(OnClicklin())
        premviewhoder.image_prem_kefu?.setOnCheckedChangeListener(OnClicklin())
        premviewhoder.image_prem_shigongdui?.setOnCheckedChangeListener(OnClicklin())
        premviewhoder.image_prem_fuguanli?.setOnCheckedChangeListener(OnClicklin())


//踢出施工队
premviewhoder.image_prem_tichu?.setOnClickListener(object :View.OnClickListener
{
    override fun onClick(v: View?) {

        val dialog = DoubleButtonDialog(content,listperm.get(position).nickname )
        dialog.getokbtn().setOnClickListener {
            dialogLoading?.show()
            if (listperm.get(position).privilegeList!=null){
                for(i in listperm.get(position).privilegeList.indices) {
                    when(listperm.get(position).privilegeList.get(i).priCode){
                        "ReportJob"->{
                            setRemoves(listperm?.get(Integer.parseInt(premviewhoder!!.image_prem_ludan?.getTag().toString()))?.loginId!!,"ReportJob")
                        }

                        "CustomSvr"->{
                            setRemoves(listperm?.get(Integer.parseInt(premviewhoder!!.image_prem_ludan?.getTag().toString()))?.loginId!!,"CustomSvr")
                        }

                        "Worker"->{
                            setRemoves(listperm?.get(Integer.parseInt(premviewhoder!!.image_prem_shigongdui?.getTag().toString()))?.loginId!!,"Worker")
                        }

                        "AssManager"->{
                            setRemoves(listperm?.get(Integer.parseInt(premviewhoder!!.image_prem_fuguanli?.getTag().toString()))?.loginId!!,"AssManager")
                        }

                    }
                }}

            ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <IAcross>{
                tichuobser->
                tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().removefromUnit(listperm.get(position).loginId))
            })).subscribe(object :Observer<IAcross>{
                override fun onNext(t: IAcross?) {
                    if (t?.m_code==200){
                        setDialogding()
                        listperm.remove(listperm.get(position))
                        notifyDataSetChanged()
                    }else{
                        setDialogding()
                        hint(t!!.m_info)
                    }
                }

                override fun onError(e: Throwable?) {

                }

                override fun onCompleted() {

                }
            })
            dialog.dismiss()
        }
        dialog.getnobtn().setOnClickListener({

            dialog.dismiss()
        })



    }
})









    }

       internal  inner class OnClicklin : CompoundButton.OnCheckedChangeListener {
        override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
                when(buttonView?.id){
                    R.id.image_prem_ludan->{
                        if (isChecked==true){
                            getAllPri(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_ludan?.getTag().toString()))?.loginId!!,"ReportJob")
                        }else{
                            setRemove(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_ludan?.getTag().toString()))?.loginId!!,"ReportJob")
                        }
                    }
                    R.id.image_prem_kefu->{
                        if (isChecked==true){
                            getAllPri(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_kefu?.getTag().toString()))?.loginId!!,"CustomSvr")
                        }else{
                            setRemove(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_kefu?.getTag().toString()))?.loginId!!,"CustomSvr")
                        }
                    }
                    R.id.image_prem_shigongdui->{
                        if (isChecked==true){
                            getShiGongDui(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_shigongdui?.getTag().toString())!!)?.loginId!!,"Worker")
                        }else{
                          setRemove(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_shigongdui?.getTag().toString())!!)?.loginId!!,"Worker")
                        }
                    }
                    R.id.image_prem_fuguanli->{
                        if (isChecked==true){
                            getAllPri(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_fuguanli?.getTag().toString())!!)?.loginId!!,"AssManager")
                        }else{
                            setRemove(listperm?.get(Integer.parseInt(premviewhoders!!.image_prem_fuguanli?.getTag().toString())!!)?.loginId!!,"AssManager")
                        }
                    }

                }

        }
    }

    inner  class  PremViewHoder(view: View){
          var text_perm_name : TextView ?= null
        var image_prem_fuguanli :CheckBox?=null
        var  image_prem_shigongdui :CheckBox?=null
        var image_prem_ludan:CheckBox?=null
        var image_prem_kefu :CheckBox?=null
        var image_prem_tichu:ImageButton?=null
        var text_perm_image  : ImageView ?= null
        init {
            text_perm_name=view.findViewById(R.id.text_perm_name) as TextView
            image_prem_fuguanli=view.findViewById(R.id.image_prem_fuguanli) as CheckBox
            image_prem_shigongdui=view.findViewById(R.id.image_prem_shigongdui) as CheckBox
            image_prem_ludan=view.findViewById(R.id.image_prem_ludan) as CheckBox
            image_prem_kefu=view.findViewById(R.id.image_prem_kefu) as CheckBox
            image_prem_tichu=view.findViewById(R.id.image_prem_tichu) as ImageButton
            text_perm_image=view.findViewById(R.id.text_perm_image) as ImageView
        }




}
    var  dialogLoading: DialogLoading?=null
    /**
     * 添加施工队的权限
     *
     */
fun  getShiGongDui(loginid:Int,code:String){

        dialogLoading?.show()
        ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <SellerEntity>{
            tichuobser->

            tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().getMSeller ())
        })).subscribe(object :Observer<SellerEntity >{
            override fun onNext(t: SellerEntity ?) {
                if (t?.m_code==200){
                    ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <ISeller>{
                        tichuobser->
                        tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().createSeller (t.sellerId,loginid))
                    })).subscribe(object :Observer<ISeller  >{
                        override fun onNext(t: ISeller  ?) {
                            if (t?.m_code==200||t?.m_code==501){
                                ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <IAcross>{
                                    tichuobser->
                                    tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().addPrivilege (0,0,loginid,code))
                                })).subscribe(object :Observer<IAcross  >{
                                    override fun onNext(t: IAcross  ?) {
                                        if (t?.m_code==200){
                                            setDialogding()
                                        }else{
                                            setDialogding()
                                            hint(t!!.m_info)

                                        }
                                    }
                                    override fun onError(e: Throwable?) {
                                    }

                                    override fun onCompleted() {
                                    }

                                })
                            }else{
                                setDialogding()
                                hint(t!!.m_info)
                            }
                        }

                        override fun onError(e: Throwable?) {
                        }

                        override fun onCompleted() {
                        }

                    })
                }else{
                    setDialogding()
                    hint(t!!.m_info)

                }
            }

            override fun onError(e: Throwable?) {

            }

            override fun onCompleted() {

            }

        })

    }



    /**
     * 添加其他权限
     *
     */
    fun getAllPri(loginid:Int,code:String){
        dialogLoading?.show()
        ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <IAcross>{
            tichuobser->
            tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().addPrivilege (0,0,loginid,code))
        })).subscribe(object :Observer<IAcross  >{
            override fun onNext(t: IAcross  ?) {
                if (t?.m_code==200){
                    setDialogding()
                }else{
                    hint(t!!.m_info)
                    setDialogding()
                }
            }

            override fun onError(e: Throwable?) {

            }

            override fun onCompleted() {

            }

        })

    }


    /**
     *删除权限
     *
     */

    fun setRemove(loginid:Int,code:String){
        dialogLoading?.show()
        ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <IAcross>{
            tichuobser->
            tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().removePrivilege (0,0,loginid,code))
        })).subscribe(object :Observer<IAcross  >{
            override fun onNext(t: IAcross  ?) {
                if (t?.m_code==200){
                    setDialogding()
                }else{
                    hint(t!!.m_info)
                    setDialogding()
                }
            }

            override fun onError(e: Throwable?) {

            }

            override fun onCompleted() {

            }

        })

    }

    /**
     *删除权限
     *
     */

    fun setRemoves(loginid:Int,code:String){
        ModeRxjavaNet.getInstantMode().objects(Observable.create(Observable.OnSubscribe <IAcross>{
            tichuobser->
            tichuobser.onNext(ModeRxjavaNet.getInstantMode().getOhInter().removePrivilege (0,0,loginid,code))
        })).subscribe(object :Observer<IAcross  >{
            override fun onNext(t: IAcross  ?) {
                if (t?.m_code==200){
                    setDialogding()


                }else{
                    hint(t!!.m_info)
                    setDialogding()
                }
            }

            override fun onError(e: Throwable?) {

            }

            override fun onCompleted() {

            }

        })

    }



    fun setDialogding() {
        if (dialogLoading != null) {
            dialogLoading!!.dismiss()
        }
    }
    //一些提示的时候调用
    fun hint(name: String) {
        val dialog = DoubleButtonDialog(content, name)
        dialog.getokbtn().setOnClickListener(View.OnClickListener { dialog.dismiss() })
        dialog.setisVisibility()

    }
    //更新适配器

    fun setNotifin(listperm: ArrayList<LoginEntity>){
        this@Perm_Manage_Adapter.listperm=listperm;
        notifyDataSetChanged()
    }


}


